﻿using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace zp.mk.TelefonLogfileHashtable
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnParse_Click(object sender, RoutedEventArgs e)
        {
            Hashtable htDur = new Hashtable();
            Hashtable htAmount = new Hashtable();

            string[,] aCont = new String[10, 3];
            int pointer = 0;
            string sOutput = String.Empty;

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Logfile (*.log)|*.log|Textdatei (*.txt)|*.txt";
            if ((bool)ofd.ShowDialog())
            {
                StreamReader sr = new StreamReader(ofd.FileName);
                txtContent.Text = sr.ReadToEnd();

                sr.Close();

                sr = new StreamReader(ofd.FileName);

                if ((bool)radioArray.IsChecked)
                {
                    while (!sr.EndOfStream)
                    {
                        string sCol = sr.ReadLine();
                        string[] sVal = sCol.Split(';');
                        bool contained = false;
                        for (int i = 0; i < aCont.GetLength(0) && !contained; i++)
                        {
                            if (aCont[i, 0] == sVal[2])
                            {
                                aCont[i, 1] = (Int32.Parse(aCont[i, 1]) + Int32.Parse(sVal[4])).ToString();
                                aCont[i, 2] = (Int32.Parse(aCont[i, 2]) + 1).ToString();
                                contained = true;
                            }
                        }
                        if (!contained)
                        {
                            aCont[pointer, 0] = sVal[2];
                            aCont[pointer, 1] = sVal[4];
                            aCont[pointer, 2] = "1";
                            pointer++;
                        }
                    }

                    for (int i = 0; i < aCont.GetLength(0); i++)
                    {
                        sOutput += aCont[i, 0] + "\t" + aCont[i, 2] + "\t" + aCont[i, 1] + "\r\n";
                    }

                    txtOutput.Text = sOutput;

                }
                else if ((bool)radioAL.IsChecked)
                {
                    while (!sr.EndOfStream)
                    {
                        string sCol = sr.ReadLine();
                        string[] sVal = sCol.Split(';');
                        
                    }
                }
                else if ((bool)radioHT.IsChecked)
                {
                    while (!sr.EndOfStream)
                    {
                        string sCol = sr.ReadLine();
                        string[] sVal = sCol.Split(';');
                        
                        if (htDur.ContainsKey(sVal[2]))
                        {
                            htDur[sVal[2]] = (int)htDur[sVal[2]] + Int32.Parse(sVal[4]);
                            htAmount[sVal[2]] = (int)htAmount[sVal[2]] + 1;
                        }
                        else
                        {
                            htDur.Add(sVal[2], Int32.Parse(sVal[4]));
                            htAmount.Add(sVal[2], 1);
                        }
                    }

                    foreach (DictionaryEntry keyvalpair in htDur)
                    {
                        htAmount[keyvalpair.Key] = (int)htAmount[keyvalpair.Key];
                        sOutput += keyvalpair.Key + "\t" + htAmount[keyvalpair.Key] + "\t" + keyvalpair.Value + Environment.NewLine;
                    }

                    txtOutput.Text = sOutput;
                }
            }
        }
    }
}